// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import infiniteScroll from 'vue-infinite-scroll'
import router from './router'
import Axios from 'axios'

Vue.use(infiniteScroll)
Vue.config.productionTip = false

var axios = Axios.create({
    // baseURL: 'http://his3.anheshou.com/api/api-ahs',
    transformRequest: [function(data) {
        // Do whatever you want to transform the data
        let ret = ''
        for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret;
    }]
});

Vue.prototype.$axios = axios;
Vue.prototype.get = function(url, parameters, sucCallBack, failCallBack, hideLoading) {
    var self = this;
    console.log("请求参数：" + JSON.stringify(parameters));
    showLoadingNative();
    // this.$axios.get(url, parameters).then(function(response) {
        this.$axios.get(path + url, parameters).then(function(response){
        dismissLoadingNative();
        // 成功回调
        var retData = JSON.parse(response.data);
        // 会话超时
        if (typeof(retData.statusCode) != 'undefined' && retData.statusCode == 1004) {
            self.$store.commit('showToast', retData.msg);
            self.$router.push({ name: 'login' });
            return;
        }
        // 接口返回错误
        if ((typeof(retData.success) != 'undefined' && retData.success != true) || (typeof(retData.error) != 'undefined' && retData.error != 0)) {
            if (typeof(retData.msg) != 'undefined') {
                self.$store.commit('showToast', retData.msg);
            }
            if (typeof(retData.message) != 'undefined') {
                self.$store.commit('showToast', retData.message);
            }
            if (failCallBack != undefined) {
                failCallBack(retData);
            }
        } else {
            // 成功回调
            console.log("返回参数：" + JSON.stringify(retData.content));
            sucCallBack(retData.content);
        }
    }).catch(function (error) {
        dismissLoadingNative();
      });
}
Vue.prototype.post = function(url, parameters, sucCallBack, failCallBack, hideLoading) {
    var self = this;
    console.log("请求参数：" + JSON.stringify(parameters));
    showLoadingNative();
    // this.$axios.post(url, parameters).then(function(response) {
        this.$axios.post(path + url, parameters).then(function (response) {
        dismissLoadingNative();
        console.log("返回参数：" + JSON.stringify(response.data));
        var retData = response.data;
        // 会话超时
        if (typeof(retData.error_code) != 'undefined' && retData.error_code != 0) {
            showErrorWithStatusNative(retData.error_msg);
            return;
        } else {
            // 成功回调
            sucCallBack(retData.data);
        }
    }).catch(function (error) {
        dismissLoadingNative();
      });
}
Vue.filter('toThousands', function(num) {
    if (typeof(num) == "number") {
        num = num + "";
    }
    var arr = num.split(".");
    num = arr[0];
    var num = (num || 0).toString(),
        result = '';
    while (num.length > 3) {
        result = ',' + num.slice(-3) + result;
        num = num.slice(0, num.length - 3);
    }
    if (num) { result = num + result; }
    if (arr[1] != undefined && arr[1] != "") {
        result = result + "." + arr[1];
    }
    return result;
});
/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    components: { App },
    template: '<App/>'
})